package com.meiyu.live.comm;

import android.content.SharedPreferences.Editor;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.netease.nimlib.sdk.auth.LoginInfo;
import com.meiyu.live.BaseApplication;
import com.meiyu.live.R;
import com.meiyu.live.popj.ActiveInfo;
import com.meiyu.live.popj.PersonalInfo;
import com.meiyu.live.popj.RelationInfo;
import com.meiyu.live.popj.UrlParams;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserConfig {

    private static final String LOGIN_ACCOUNT = "login_account";
    private static final String LOGIN_PASSWORD = "login_password";
    private static final String PERSONAL_INFO = "personal_info";
    private static final String ACTIVE_INFO = "active_info";
    private static final String RELATION_INFO = "relation_info";
    private static final String USER_COINS = "user_coins";

    private static final String SESSION_TOKEN = "session_token";
    private static final String NET_EASE = "net_ease";
    private static final String NET_TOKEN = "net_token";
    private static final String FLAG_LOGIN_NO = "flag_login_no";
    private static final String RECENT_SEARCH = "recnet_search";
    private static final String USERID = "userid";
    private static final String OPENID = "openid";
    private static final String SEX = "sex";
    private static final String UID = "uid";
    private static final String THIRDTOKEN = "thirdtoken";
    private static final String THIRD_PLATFORM = "third_platform";
    private static final String THIRD_NAME = "third_name";
    private static final String THIRD_AVATAR = "third_avartar";

    private static final String SETTINGS_NOTIFY_ANCHOR_STARTLIVE = "settings_notify_anchor_startlive";
    private static final String GUIDANCE_SHOW_VERSION = "guidance_show_version";//记录引导页显示的版本

    private static UserConfig instance;
    private String session_token;
    private String net_ease;
    private String net_token;
    private long userId = 0;
    private LoginInfo loginInfo;
    private String openId;
    private String thirdTocken;
    private String sex;
    private String uid;
    private UrlParams params;
    private PersonalInfo personalInfo;
    private RelationInfo relationInfo;
    private List<ActiveInfo> activeInfos;
    public static Map<String,Integer> puke = new HashMap<>();
    public static Map<String,Integer> niu = new HashMap<>();
    static
    {
        puke.put("1", R.drawable._1);
        puke.put("2", R.drawable._2);
        puke.put("3", R.drawable._3);
        puke.put("4", R.drawable._4);
        puke.put("5", R.drawable._5);
        puke.put("6", R.drawable._6);
        puke.put("7", R.drawable._7);
        puke.put("8", R.drawable._8);
        puke.put("9", R.drawable._9);
        puke.put("10", R.drawable._10);
        puke.put("11", R.drawable._11);
        puke.put("12", R.drawable._12);
        puke.put("13", R.drawable._13);
        puke.put("14", R.drawable._14);
        puke.put("15", R.drawable._15);
        puke.put("16", R.drawable._16);
        puke.put("17", R.drawable._17);
        puke.put("18", R.drawable._18);
        puke.put("19", R.drawable._19);
        puke.put("20", R.drawable._20);
        puke.put("21", R.drawable._21);
        puke.put("22", R.drawable._22);
        puke.put("23", R.drawable._23);
        puke.put("24", R.drawable._24);
        puke.put("25", R.drawable._25);
        puke.put("26", R.drawable._26);
        puke.put("27", R.drawable._27);
        puke.put("28", R.drawable._28);
        puke.put("29", R.drawable._29);
        puke.put("30", R.drawable._30);
        puke.put("31", R.drawable._31);
        puke.put("32", R.drawable._32);
        puke.put("33", R.drawable._33);
        puke.put("34", R.drawable._34);
        puke.put("35", R.drawable._35);
        puke.put("36", R.drawable._36);
        puke.put("37", R.drawable._37);
        puke.put("38", R.drawable._38);
        puke.put("39", R.drawable._39);
        puke.put("40", R.drawable._40);
        puke.put("41", R.drawable._41);
        puke.put("42", R.drawable._42);
        puke.put("43", R.drawable._43);
        puke.put("44", R.drawable._44);
        puke.put("45", R.drawable._45);
        puke.put("46", R.drawable._46);
        puke.put("47", R.drawable._47);
        puke.put("48", R.drawable._48);
        puke.put("49", R.drawable._49);
        puke.put("50", R.drawable._50);
        puke.put("51", R.drawable._51);
        puke.put("52", R.drawable._52);
        niu.put("0", R.drawable.niu_0);
        niu.put("1", R.drawable.niu_1);
        niu.put("2", R.drawable.niu_2);
        niu.put("3", R.drawable.niu_3);
        niu.put("4", R.drawable.niu_4);
        niu.put("5", R.drawable.niu_5);
        niu.put("6", R.drawable.niu_6);
        niu.put("7", R.drawable.niu_7);
        niu.put("8", R.drawable.niu_8);
        niu.put("9", R.drawable.niu_9);
        niu.put("10", R.drawable.niu_10);
    }

    private UserConfig() {
    }

    public UrlParams getParams() {
        return params;
    }

    public void setParams(UrlParams params) {
        this.params = params;
    }

    public LoginInfo getIMLoginInfo() {
        return loginInfo;
    }

    public long getUserId() {
        if (userId == 0) {
            this.userId = BaseApplication.getApp().getShared().getLong(USERID, 0l);
        }
        return userId;
    }

    public void setUserId(long userId) {
        this.userId = userId;
        BaseApplication.getApp().getShared().edit().putLong(USERID, userId).commit();
    }

    public String getOpenId() {
        if (TextUtils.isEmpty(this.openId)) {
            this.openId = BaseApplication.getApp().getShared().getString(OPENID, "");
        }
        return openId;
    }

    public void setOpenId(String openId) {
        this.openId = openId;
        BaseApplication.getApp().getShared().edit().putString(OPENID, openId).commit();
    }

    public String getSex() {
        if (TextUtils.isEmpty(this.openId)) {
            this.sex = BaseApplication.getApp().getShared().getString(SEX, "");
        }
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
        BaseApplication.getApp().getShared().edit().putString(SEX, sex).commit();
    }

    public String getUid() {
        if (TextUtils.isEmpty(uid)) {
            this.uid = BaseApplication.getApp().getShared().getString(UID, "");
        }
        return uid;
    }

    public void setUid(String uid) {
        this.uid = uid;
        BaseApplication.getApp().getShared().edit().putString(UID, uid).commit();
    }

    public String getThirdTocken() {
        if (TextUtils.isEmpty(this.thirdTocken)) {
            this.thirdTocken = BaseApplication.getApp().getShared().getString(THIRDTOKEN, "");
        }
        return thirdTocken;
    }

    public void setThirdTocken(String thirdTocken) {
        this.thirdTocken = thirdTocken;
        BaseApplication.getApp().getShared().edit().putString(THIRDTOKEN, thirdTocken).commit();
    }

    public String getThirdPlatform() {
        return BaseApplication.getApp().getShared().getString(THIRD_PLATFORM, "");
    }

    public void setThirdPlatform(String thirdPlatform) {
        BaseApplication.getApp().getShared().edit().putString(THIRD_PLATFORM, thirdPlatform).commit();
    }

    public String getThirdName() {
        return BaseApplication.getApp().getShared().getString(THIRD_NAME, "");
    }

    public void setThirdName(String thirdName) {
        BaseApplication.getApp().getShared().edit().putString(THIRD_NAME, thirdName).commit();
    }

    public String getThirdAvatar() {
        return BaseApplication.getApp().getShared().getString(THIRD_AVATAR, "");
    }

    public void setThirdAvatar(String thirdAvatar) {
        BaseApplication.getApp().getShared().edit().putString(THIRD_AVATAR, thirdAvatar).commit();
    }

    public void setIMLoginInfo(LoginInfo loginInfo) {
        this.loginInfo = loginInfo;
    }

    public String getSession_token() {
        if (TextUtils.isEmpty(session_token)) {
            this.session_token = BaseApplication.getApp().getShared().getString(SESSION_TOKEN, "");
        }
        return session_token;
    }

    public void setSession_token(String session_token) {
        this.session_token = session_token;
        BaseApplication.getApp().getShared().edit().putString(SESSION_TOKEN, session_token).commit();
    }

    public String getNet_ease() {
        if (TextUtils.isEmpty(this.net_ease)) {
            this.net_ease = BaseApplication.getApp().getShared().getString(NET_EASE, "");
        }
        return net_ease;
    }

    public void setNet_ease(String net_ease) {
        this.net_ease = net_ease;
        BaseApplication.getApp().getShared().edit().putString(NET_EASE, net_ease).commit();
    }

    public String getNet_token() {
        if (TextUtils.isEmpty(this.net_token)) {
            this.net_token = BaseApplication.getApp().getShared().getString(NET_TOKEN, "");
        }
        return net_token;
    }

    public void setNet_token(String net_token) {
        this.net_token = net_token;
        BaseApplication.getApp().getShared().edit().putString(NET_TOKEN, net_token).commit();
    }

    public String getLogingAccount() {
        return BaseApplication.getApp().getShared().getString(LOGIN_ACCOUNT, "");
    }

    public void setLogingAccount(String logingAccount) {
        BaseApplication.getApp().getShared().edit().putString(LOGIN_ACCOUNT, logingAccount).commit();
    }

    public String getLogingPassword() {
        return BaseApplication.getApp().getShared().getString(LOGIN_PASSWORD, "");
    }

    public void setLogingPassword(String logingPassword) {
        BaseApplication.getApp().getShared().edit().putString(LOGIN_PASSWORD, logingPassword).commit();
    }

    public PersonalInfo getPersonalInfo() {
        if (personalInfo == null) {
            String json = BaseApplication.getApp().getShared().getString(PERSONAL_INFO, "");
            try {
                personalInfo = new Gson().fromJson(json, PersonalInfo.class);
            } catch (JsonSyntaxException e) {
                e.printStackTrace();
            }
        }
        return personalInfo;
    }

    public void setPersonalInfo(PersonalInfo personalInfo) {
        this.personalInfo = personalInfo;
        if (personalInfo == null) {
            BaseApplication.getApp().getShared().edit().remove(PERSONAL_INFO).commit();
        } else {
            BaseApplication.getApp().getShared().edit().putString(PERSONAL_INFO, new Gson().toJson(personalInfo)).commit();
        }
    }

    public List<ActiveInfo> getActiveInfos() {
        if (activeInfos == null) {
            String json = BaseApplication.getApp().getShared().getString(ACTIVE_INFO, "");
            try {
                activeInfos = new Gson().fromJson(json, new TypeToken<ArrayList<ActiveInfo>>() {
                }.getType());
            } catch (JsonSyntaxException e) {
                e.printStackTrace();
            }
        }
        return activeInfos;
    }

    public void setActiveInfos(List<ActiveInfo> activeInfos) {
        this.activeInfos = activeInfos;
        if (activeInfos == null) {
            BaseApplication.getApp().getShared().edit().remove(ACTIVE_INFO).commit();
        } else {
            BaseApplication.getApp().getShared().edit().putString(ACTIVE_INFO, new Gson().toJson(activeInfos)).commit();
        }
    }

    public RelationInfo getRelationInfo() {
        if (personalInfo == null) {
            String json = BaseApplication.getApp().getShared().getString(RELATION_INFO, "");
            try {
                relationInfo = new Gson().fromJson(json, RelationInfo.class);
            } catch (JsonSyntaxException e) {
                e.printStackTrace();
            }
        }
        return relationInfo;
    }

    public void setRelationInfo(RelationInfo relationInfo) {
        this.relationInfo = relationInfo;
        if (personalInfo == null) {
            BaseApplication.getApp().getShared().edit().remove(RELATION_INFO).commit();
        } else {
            BaseApplication.getApp().getShared().edit().putString(RELATION_INFO, new Gson().toJson(relationInfo)).commit();
        }
    }

    public int getUserCoins() {
        return BaseApplication.getApp().getShared().getInt(USER_COINS, 0);
    }

    public void setUserCoins(int coins) {
        BaseApplication.getApp().getShared().edit().putInt(USER_COINS, coins).commit();
    }

    public boolean getSettingsNotifyAnchorStartlive() {
        return BaseApplication.getApp().getShared().getBoolean(SETTINGS_NOTIFY_ANCHOR_STARTLIVE, true);
    }

    public void setSettingsNotifyAnchorStartlive(boolean isNotify) {
        BaseApplication.getApp().getShared().edit().putBoolean(SETTINGS_NOTIFY_ANCHOR_STARTLIVE, isNotify).commit();
    }

    public static final UserConfig getInstance() {
        if (instance == null) {
            instance = new UserConfig();
        }
        return instance;
    }

    public final void unlogin() {
        instance = new UserConfig();
        Editor editor = BaseApplication.getApp().getShared().edit();
        editor.remove(SESSION_TOKEN);
        editor.remove(NET_EASE);
        editor.remove(FLAG_LOGIN_NO);
        editor.remove(NET_TOKEN);
        editor.remove(OPENID);
        editor.remove(THIRD_PLATFORM);
        editor.remove(THIRD_NAME);
        editor.remove(THIRD_AVATAR);
        editor.remove(THIRDTOKEN);
        editor.remove(USERID);

        editor.remove(LOGIN_ACCOUNT);
        editor.remove(LOGIN_PASSWORD);
        editor.remove(SEX);
        editor.remove(SETTINGS_NOTIFY_ANCHOR_STARTLIVE);
        editor.remove(PERSONAL_INFO);
        editor.remove(ACTIVE_INFO);
        editor.remove(RELATION_INFO);
        editor.remove(RECENT_SEARCH);
        editor.remove(USER_COINS);
        editor.remove(UID);

        editor.commit();
    }

    public boolean isLogin() {
        return !TextUtils.isEmpty(getSession_token());
    }

    /**
     * 获取最近搜索
     */
    public String getRecentSearch() {
        return BaseApplication.getApp().getShared().getString(RECENT_SEARCH, "[]");
    }

    /**
     * 最近搜索
     */
    public void setRecentSearch(String recentSearchJson) {
        BaseApplication.getApp().getShared().edit().putString(RECENT_SEARCH, recentSearchJson).commit();
    }

    public int getGuidanceShowVersion() {
        return BaseApplication.getApp().getShared().getInt(GUIDANCE_SHOW_VERSION, 0);
    }

    public void setGuidanceShowVersion(int versionCode) {
        BaseApplication.getApp().getShared().edit().putInt(GUIDANCE_SHOW_VERSION, versionCode).commit();
    }
}
